#!/bin/sh

# Copyright (c) 2018
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Author: Mathias Ettinger <mettinger@toulouse.viveris.fr>
#

#
# Hook to check for NS-3 coding style violations.
#
# To enable this hook, move it to the .git/hooks folder
# and rename the file to "pre-commit".

# Redirect output to stderr.
exec 1>&2

# Be sure to check only stuff that has been added to the index
git stash -q --keep-index

# If there are coding-style errors in source files, print the offending file names and fail.
ext-utils/coding_style.sh git-check 
result=$?

# Restore working version
git stash pop -q

# Check whether the user whish to commit when coding-style errors are present
if [ $result -ne 0 ]
then
  echo
  read -n 1 -p 'Commit anyway? [y/N] ' answer < /dev/tty
  if [ "Y$answer" != "Y" ]; then echo; fi
  if [ "Y$answer" != "Yy" ] && [ "Y$answer" != "YY" ]
  then
    exit 1
  else
    echo -e "\nApplying commit anyway:\n"
  fi
fi

exit 0
